	Ext.BLANK_IMAGE_URL = './ext-3.2/resources/images/default/s.gif';
	Ext.onReady(function(){
		
		
	});

/** =======================================================
 *  ExtJs Functions
 ** ==================================================== */
 	function ExtAlert(msg){
		Ext.Msg.alert("友情提示：",msg);
	}
	
	function renderFileType(v){
		if(v=="pdf") v="<img src=\"./images/pdm/pdf_icon.png\" alt=\"在线阅读文档\"/>";
		return v;
	}
	
	function openDc(v, screen_ID){
		jsonrpc = new JSONRpcClient("/pdm/JSON-RPC");
		try{
			var info = jsonrpc.jabsorbService.getDispatchUrl(v, Ext.fly("login_user").dom.value, screen_ID);
		}catch(e){
			alert(e);
		}
		
		window.open("http://myivo.ivo.com.cn/Arcadia/servlet/WebDispatcher?mailCode="+info);
	}
	
/** =======================================================
 *  save pages info
 ** ==================================================== */
	function saveInfo(tmpl_id, mast_id, bEnd)
	{
		var nInvalid = 0;
		$("form").each(function(id){
			input_items = $("input[class*=cls_validator_error]");
			nInvalid += input_items.length;
			textarea_items = $("textarea[class*=cls_validator_error]");
			nInvalid += textarea_items.length;
		});
		
		var dirty_item = $("#pdm_dirty_datas").attr("value");
		
		$("[class*=jquery-ux-input-dirty]").each(function(a){
			if(dirty_item=="") dirty_item = $(this).attr("id");
			else dirty_item = dirty_item+","+$(this).attr("id");
			$("#pdm_dirty_datas").attr("value", dirty_item);
		});
				
		if(nInvalid==0)
		{
			var conclu = $('input[name=conclusion]:checked').val();
			var conclu_hide = $('#conclusion_tabs :hidden');
			
			var rsn = $("#reason").attr("value");
			var links = $("#_collectDocument").attr("value");
			
			if( !conclu_hide && typeof(conclu)=='undefined' ) ExtAlert("请选择结论！");
			else if( conclu=="conditional" && rsn=="" ) ExtAlert("结论栏您选择了“有条件的”，请不要忘记填写条件内容！");
			else if( bEnd == 'true' )
			{
				var furValue = $("#btn_furSelection").parent().prev().find("input").attr("value");
				var matValue = $("#div_selected_material_grid").parent().parent().prev().find("input").attr("value");
				var gpValue = $("#div_gp_param_grid").parent().parent().prev().find("input").attr("value");
				var delayValue = $("#btn_select_fur").parent().prev().find("input").attr("value");
				
				if( furValue != null && (furValue.trim() == "" || furValue.trim() == "null") ) Ext.Msg.alert("提示","请选择FUR单号");
				else if( matValue != null && (matValue.trim() == "" || matValue.trim() == "null") ) Ext.Msg.alert("提示","请选择BOM的相关信息");
				else if( gpValue != null && (gpValue.trim() == "" || gpValue.trim() == "null") ) Ext.Msg.alert("提示","请选择GP的相关信息");
				else if( delayValue != null && (delayValue.trim() == "" || delayValue.trim() == "null") ) Ext.Msg.alert("提示","请选择FUR Item的相关信息");
				else {
					var options = {
						url: '/pdm/page/saveInfo.htm?tmpl_id='+tmpl_id+'&mast_id='+mast_id,
						data: { conclusion: conclu, reason: rsn, documents: links, endFlag: bEnd },
						type: 'POST',
						success: function(){
							window.location.reload(true);
						},
						error: function(){
					    	Ext.Msg.alert("提示","保存失败。");
						}
					};
					$('#basic_form').ajaxSubmit(options);
				
				}
			}
			else {
				var options = {
					url: '/pdm/page/saveInfo.htm?tmpl_id='+tmpl_id+'&mast_id='+mast_id,
					data: { conclusion: conclu, reason: rsn, documents: links, endFlag: bEnd },
					type: 'POST',
					success: function(){
						window.location.reload(true);
					},
					error: function(){
				    	Ext.Msg.alert("提示","保存失败。");
					}
				};
				$('#basic_form').ajaxSubmit(options);
			}
		}
		else Ext.Msg.alert("提示","请确认红色框框内的数据是否符合要求！<br/>点击小图标可以查看相关信息！");
		
	}
/** =======================================================
 *  form validation
 ** ==================================================== */	
	function formValidate(){
		$("[validator]").each(function(i){
			var valobj = CreateValObject($(this).attr('validator')); 
			if(valobj.nullable == "false") 
			{
				if($.trim($('#' + $(this).attr('id')).attr("value")) == "")
				{
					$('#' + $(this).attr('id')).addClass('cls_validator_error');
					$('#' + $(this).attr('id')).after("<img id=\""+$(this).attr('id')+"_alert\" src=\"./images/pdm/exclamation_diamond_frame.png\" alt=\"此项不可为空\" title=\"此项不可为空\" />");
				}
			}
			if(valobj.readOnly) $('#' + $(this).attr('id')).attr('readonly',true);
			
			$(this).blur(function(){
				var _input_id = $(this).attr('id');
				var validateResult = OnItemValidator($(this));
				
				if(validateResult.result)
				{
					$('#' + _input_id).removeClass('cls_validator_error');
					$('#' + _input_id + '_alert').attr("src","./ext-3.2/resources/images/default/s.gif");
					$('#' + _input_id + '_alert').attr("alt","");
					$('#' + _input_id + '_alert').attr("title","");
				}
				else
				{
					$('#' + _input_id).addClass('cls_validator_error');
					var _img = $('#' + _input_id + '_alert');
					if(_img.attr("id")==null||_img.attr("id")=="undefined"){
						$('#' + _input_id).after("<img id=\""+_input_id+"_alert\" src=\"./images/pdm/exclamation_diamond_frame.png\" alt=\""+validateResult.resultMsg+"\" title=\""+validateResult.resultMsg+"\" />");
					}else{
						if($('#' + _input_id + '_alert').attr("src")=="./ext-3.2/resources/images/default/s.gif"){
							$('#' + _input_id + '_alert').attr("src","./images/pdm/exclamation_diamond_frame.png");
							$('#' + $(this).attr('id')+"_alert").attr("alt",validateResult.resultMsg);
							$('#' + $(this).attr('id')+"_alert").attr("title",validateResult.resultMsg);
						}
					}
				}
			});
			
			$(this).change(function(){
				var _input_id = $(this).attr('id');
				$('#' + _input_id).addClass('jquery-ux-input-dirty');
			});
		});
		
		$("form :radio").each(function(i){
			$(this).change(function(){
				var _input_id = $(this).attr('id');
				$('#' + _input_id).addClass('jquery-ux-input-dirty');
			});
		});
	}
	
/** =======================================================
 *  Create Follow Up Button
 ** ==================================================== */	
	function createFollowUpButton(win, name, value, bAuth, basePath){
		var button = new Ext.Button({
			text: "选择...",
			scope: this,
			hidden: (bAuth=="false"),
			handler: function() {
				if(Ext.isEmpty(win)){
					var furSelectionGrid = new FURSelectionGridPanel({txtfld: name});
					win = new FURSelectionWindow({id: "ext_selectionWin", title: "FUR选择窗口", furSelectionGrid: furSelectionGrid, txtfld: name});
				}
				win.show();
				win.load();
			}
		});
		
		var btn_reset = new Ext.Button({
			text: "重置",
			scope: this,
			hidden: (bAuth=="false"),
			handler: function() {
				Ext.fly(name).dom.value = " ";
				
				var panel = Ext.getCmp("ext_furInfo");
				panel.destroy();
				
				panel = new Ext.Panel({
					id: "ext_furInfo",
					title: "申请单信息",
					height: 480,
					autoDestroy: true,
					html: '<iframe src="'+basePath+'images/app/nothingfound.png" width="100%" height="100%" frameborder=1 scrolling=auto></iframe>'
				});
				
				panel.render("div_FurInfo");
			}
		});
		
		var panel = null;
		if(value.trim()!="")
		{
			jsonrpc = new JSONRpcClient("/pdm/JSON-RPC");
			try{
				var info = jsonrpc.jabsorbService.getDispatchUrl(value, Ext.fly("login_user").dom.value,"PM.FUR");
			}catch(e){
				alert(e);
			}
			panel = new Ext.Panel({
				id: "ext_furInfo",
				title: "页面信息",
				height: 480,
				autoDestroy: true,
				html: '<iframe src="http://myivo.ivo.com.cn/Arcadia/servlet/WebDispatcher?mailCode='+info+'" width="100%" height="100%" frameborder=1 scrolling=auto></iframe>'
			});
			panel.setTitle("[" + value + "] 页面信息");
			
		}else{
			panel = new Ext.Panel({
				id: "ext_furInfo",
				title: "申请单信息",
				height: 480,
				autoDestroy: true,
				html: '<iframe src="'+basePath+'/images/app/nothingfound.png" width="100%" height="100%" frameborder=1 scrolling=auto></iframe>'
			});
		}
		
		panel.render("div_FurInfo");
		button.render("btn_furSelection");
		btn_reset.render("btn_reset");
		
	}
/** =======================================================
 *  Create BOM Report
 ** ==================================================== */
	function createBoMReport(win, name, value, bAuth, basePath){
		
		var grid = new BomReportGridPanel({id: 'gp_BomReport', title: "相关BOM信息"});
		var selectedMaterial = new SelectedMaterialGrid({id: 'gp_SelectedMaterial', btn_auth: bAuth, hidden_BoMParam: name});
		
		grid.render("div_bom_grid");
		selectedMaterial.render("div_selected_material_grid");
		
		grid.load(value);
		selectedMaterial.load(value);
	}
	
/** =======================================================
 *  Create DC Report
 ** ==================================================== */
	function createDcReport(win, name, value, bAuth, basePath){
		
		$("#minDateOfECR").datepicker();
		$('#minDateOfECR').datepicker('option', {dateFormat: "yy-mm-dd"});
		
		$("#maxDateOfECR").datepicker();
		$('#maxDateOfECR').datepicker('option', {dateFormat: "yy-mm-dd"});
		
		var paramJson = $("#"+name).attr("value");
		var param = "";
		if( paramJson!= "" )
		{
			var obj = eval("("+paramJson+")");
			
			$("#minDateOfECR").attr("value", obj.minDate);
			$("#maxDateOfECR").attr("value", obj.maxDate);
			
			if(obj.productType=="002"){
				$("#productType_Cell").attr("checked",true);
			}
			else if(obj.productType=="004"){
				$("#productType_Module").attr("checked",true);
			}
			else if(obj.productType=="002,004"){
				$("#productType_Cell").attr("checked",true);
				$("#productType_Module").attr("checked",true);
			}
			
			param = "?minDate="+obj.minDate+"&maxDate="+obj.maxDate+"&productType="+obj.productType+"&productModel_FK="+obj.productModel_FK;
			
			$.ajax({
				type: "post",
				cache: "false",
				url: "/pdm/report/dcReport.htm"+param,
				dataType: "html",
				contentType: "application/html; charset=utf-8",
				timeout: 20000,
				error: function(){
				    alert("查询出问题，请联系管理员！");
				},
				success: function(html){
					$("#dc_result_table tbody").empty();
					
					if(html != "\r\n")
					{
						$("#dc_result_table tbody").append(html);
						$("#dc_result_table").trigger("update");
						var sorting = [[2,0]];
						$("#dc_result_table").trigger("sorton",[sorting]);
					}
				}
			});
		}
		
		$("#dc_result_table").tablesorter({widgets: ['zebra']});
		
		if(bAuth=="false") $("#btn_dc_query").hide();
		
		$("#btn_dc_query").click(function() {
			
			paramJson = $("#"+name).attr("value");
			
			var minDateOfECR = $("#minDateOfECR").attr("value");
			var maxDateOfECR = $("#maxDateOfECR").attr("value");
			
			var productType_Cell = $("#productType_Cell[checked]").attr("value");
			var productType_Module = $("#productType_Module[checked]").attr("value");
			
			var productModel_FK = $("#projectName").attr("value");
			
			var productType = "";
			if(productType_Cell==null)
			{
				if(productType_Module!=null) productType = productType_Module;
			}else{
				if(productType_Module == null) productType = productType_Cell;
				else productType = productType_Cell+","+productType_Module;
			}
			
			param = "?minDate="+minDateOfECR+"&maxDate="+maxDateOfECR+"&productType="+productType+"&productModel_FK="+productModel_FK;
			paramJson = "{'minDate':'"+minDateOfECR+"','maxDate':'"+maxDateOfECR+"','productType':'"+productType+"','productModel_FK':'"+productModel_FK+"'}";
			
			$("#"+name).attr("value", paramJson)
			
			if(productType_Cell==null&&productType_Module==null&&minDateOfECR==""&&maxDateOfECR==""){
				alert("请输入查询条件");
			}else{
				$.ajax({
					type: "post",
					cache: "false",
					url: "/pdm/report/dcReport.htm"+param,
					dataType: "html",
					contentType: "application/html; charset=utf-8",
					timeout: 20000,
					error: function(){
					    alert("查询出问题，请联系管理员！");
					},
					success: function(html){
						$("#dc_result_table tbody").empty();
						if(html != "\r\n")
						{
						    $("#dc_result_table tbody").append(html);
							$("#dc_result_table").trigger("update");
							var sorting = [[2,0]];
							$("#dc_result_table").trigger("sorton",[sorting]);
						}
					}
				});
				return false;
			}
		});		
	}
/** =======================================================
 *  Create GP Report
 ** ==================================================== */
	function createGpReport(win, name, value, bAuth, basePath){
		
		var grid = new GpReportGridPanel({id: 'gp_gpReport', title: "相关GP信息"});
		var paramGrid = new GpParamGrid({id: 'gp_GpParam', btn_auth: bAuth, hidden_GpParam: name});
		
		paramGrid.render("div_gp_param_grid");
		grid.render("div_gp_info_grid");
		
		if(value == "") value="[]";
		
		grid.load(value);
		paramGrid.load(value);
	}
	
/** =======================================================
 *  Create FUR Delay Details
 ** ==================================================== */
	function createFurDelay(win, name, value, bAuth, basePath){
		
		var button = new Ext.Button({
			text: "选择...",
			scope: this,
			hidden: (bAuth=='false'),
			handler: function() {
				if(Ext.isEmpty(win)){
					var furListGridPanel = new FurListGridPanel();
					win = new FurListWindow({id: "win_FurList", title: "FUR选择窗口", furListGrid: furListGridPanel, keyName: name});
				}
				win.show();
				win.load();
			}
		});
		
		if(Ext.fly("btn_select_fur").dom.value) button.render("btn_select_fur");
		
		if(value!="")
		{
			var param = "?keyNumber="+value;
						
			$.ajax({
				type: "post",
				cache: "false",
				url: "/pdm/report/furDelayItem.htm"+param,
				dataType: "json",
				contentType: "application/json; charset=utf-8",
				timeout: 20000,
				error: function(){
				    $("#pdm_div_overDue").empty();
					$("#pdm_div_overDue").append("<font color=red>载入信息失败，请重新选择</font>");
				},
				success: function(json){
					$("#pdm_div_overDue").empty();
					$("#pdm_div_overDue").append("逾期数量：(  <a href=\"javascript:openDelayItems('"+value+"')\"><font color=red>"+json[0].delayNumber+"</font></a>  )项");
				}
			});	
		}else{
			$("#pdm_div_overDue").empty();
			$("#pdm_div_overDue").append("逾期数量：(  <a href=\"javascript:openDelayItems('')\"><font color=red>0</font></a>  )项");
		}
	}
	
	function openDelayItems(v)
	{
		if(v==""){
			Ext.Msg.alert("提示","没有逾期项目");
		}else{
			var gp = Ext.getCmp("pm_furDetails");
			if(Ext.isEmpty(gp)) gp = new FurDetailsGridPanel({id: 'pm_furDetails'});
			
			var w = Ext.getCmp("win_pm_furDetails");
			if(Ext.isEmpty(w)) w = new FurDetailsWindow({id: 'win_pm_furDetails',title: "逾期事项", furDetailsGrid: gp});
			w.show();
			w.load(v);
		}
	}
	
/** =======================================================
 *  Create Material Accept Report
 ** ==================================================== */
	function createMaReport(win, name, value, bAuth, basePath){
		
		var grid = new MaReportGridPanel({id: 'gp_MaReport', title: "相关物料承认信息"});
		var paramGrid = new MaParamGrid({id: 'gp_MaParam', btn_auth: bAuth, hidden_MaParam: name});
		
		paramGrid.render("div_ma_param_grid");
		grid.render("div_ma_info_grid");
		
		if(value == "") value="[]";
		
		grid.load(value);
		paramGrid.load(value);
	}
	
	
	String.prototype.trim=function(){
		return this.replace(/(^\s*)|(\s*$)/g, "");
	}
	String.prototype.ltrim=function(){
		return this.replace(/(^\s*)/g,"");
	}
	String.prototype.rtrim=function(){
		return this.replace(/(\s*$)/g,"");
	}